package com.third.clock.ui.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.third.clock.MainActivity;
import com.third.clock.R;
import com.third.clock.bean.MediaEntity;
import com.third.clock.bean.NoticeEntity;
import com.third.clock.common.LogUtils;
import com.third.clock.manager.ImgaeEngineManager;
import com.third.clock.manager.TransformManager;
import com.third.clock.manager.WindowMessageManager;
import com.third.clock.ui.AttendanceActivity;
import com.third.clock.ui.ImageBrowserActivity;
import com.third.clock.ui.LeaveActivity;
import com.third.clock.ui.LoginActivity;
import com.third.clock.ui.NoticeDetailsActivity;
import com.third.clock.ui.NoticeListActivity;
import com.third.clock.ui.model.IMainFragmentView;
import com.third.clock.ui.model.presenter.MainFragmentPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/23 0023.
 */

public class MainFragment extends Fragment implements IBaseFragment,IMainFragmentView,View.OnClickListener{

    private static final String TAG="MainFragment";

    protected Activity ctx;

    protected View layout;

    private ImageView mImageViewClock;

    private ImageView mImageViewNotice;

    private TextView mTextviewleave;

    private TextView mTextviewAttendance;

    private TextView mTextViewNoticeTitle;

    private TextView mTextViewNoticeTime;

    private TextView mTextViewNoticeContent;

    private TextView mTextViewTitle;

    private TextView mTextViewRight;

    private View mHeadView;

    private View mNoticeView;

    private View mImageViewLayout;

    private MainFragmentPresenter mainFragmentPresenter;

    private NoticeEntity mNoticeEntity;

    private ImgaeEngineManager imgaeEngineManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (layout == null) {
            ctx = this.getActivity();
            layout = ctx.getLayoutInflater().inflate(R.layout.fragment_main, null);

        } else {
            ViewGroup parent = (ViewGroup) layout.getParent();
            if (parent != null) {
                parent.removeView(layout);
            }
        }
        return layout;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        initEvent();
        LogUtils.e(TAG,"onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        WindowMessageManager.getInstance().dismiss();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void initView(){
        mTextviewleave= (TextView) layout.findViewById(R.id.textview_leave);
        mTextviewAttendance=(TextView)layout.findViewById(R.id.textview_attendance);
        mImageViewClock=(ImageView) layout.findViewById(R.id.imageview_clock);
        mTextViewNoticeTitle= (TextView) layout.findViewById(R.id.textview_notice_title);
        mTextViewNoticeTime=(TextView) layout.findViewById(R.id.textview_notice_time);
        mTextViewNoticeContent=(TextView) layout.findViewById(R.id.textview_notice_content);
        mImageViewNotice=(ImageView) layout.findViewById(R.id.imageview_notice);
        mImageViewLayout=layout.findViewById(R.id.imageview_notice_layout);
        mHeadView=layout.findViewById(R.id.main_head_layout);
        mTextViewTitle= (TextView) mHeadView.findViewById(R.id.textview_head_title);
        mTextViewRight= (TextView) mHeadView.findViewById(R.id.textview_head_right);
        mNoticeView=layout.findViewById(R.id.notice_layout);

        mTextviewleave.setOnClickListener(this);
        mTextviewAttendance.setOnClickListener(this);
        mImageViewClock.setOnClickListener(this);
        mImageViewNotice.setOnClickListener(this);
        mTextViewRight.setOnClickListener(this);
        mNoticeView.setOnClickListener(this);

        mainFragmentPresenter=new MainFragmentPresenter(getActivity(),this);
        mTextViewTitle.setText("主页");
        mTextViewRight.setVisibility(View.VISIBLE);

        imgaeEngineManager=new ImgaeEngineManager();
    }

    @Override
    public void initEvent() {
           if(mainFragmentPresenter!=null) mainFragmentPresenter.getFirstNotice();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.textview_leave:
                startActivity(new Intent(getActivity(), LeaveActivity.class));
                break;
            case R.id.textview_attendance:
                startActivity(new Intent(getActivity(), AttendanceActivity.class));
                break;
            case R.id.imageview_clock:
                mainFragmentPresenter.actionClock(1);
                break;
            case R.id.imageview_notice:
                if(mNoticeEntity!=null) {
                    ImageBrowserActivity.intentData(getActivity(),mNoticeEntity.getMedias(),0 );
                }
                break;
            case R.id.textview_head_right:
                startActivity(new Intent(getActivity(), NoticeListActivity.class));
                break;
            case R.id.notice_layout:
                if(mNoticeEntity!=null){
                    NoticeDetailsActivity.intentData(getActivity(),mNoticeEntity);
                }
                break;
        }
    }

    @Override
    public void onClockFail(String message) {
        if(!TextUtils.isEmpty(message)&&getActivity()!=null&&!getActivity().isFinishing()){
            WindowMessageManager.getInstance().showInfoWithStatus(getActivity(),message);
        }
    }

    @Override
    public void onClockSuccess() {
        if(getActivity()!=null&&!getActivity().isFinishing()) {
            WindowMessageManager.getInstance().showSuccessWithStatus(getActivity(),"打卡成功");
        }
    }

    @Override
    public void onFirstNotice(NoticeEntity noticeEntity) {
        if(noticeEntity!=null){
            mNoticeEntity=noticeEntity;
            String title=noticeEntity.getTitle();
            if(!TextUtils.isEmpty(title)) {
                mTextViewNoticeTitle.setText(title);
            }
            String time=noticeEntity.getCdate();
            if(!TextUtils.isEmpty(time)) {
                try{
                    mTextViewNoticeTime.setText(TransformManager.paserLongToStringDate("yyyy-MM-dd HH:mm",Long.parseLong(time)*1000));
                }catch (Exception e){
                    mTextViewNoticeTime.setText("");
                }
            }
            String conent=noticeEntity.getContent();
            if(!TextUtils.isEmpty(conent) ){
                mTextViewNoticeContent.setText(conent);
            }
            List<MediaEntity> list=noticeEntity.getMedias();
            if(list!=null&&list.size()!=0){
                imgaeEngineManager.loadImage(getActivity(),list.get(0).getUrl(),mImageViewNotice);
                mImageViewLayout.setVisibility(View.VISIBLE);
            }else{
                mImageViewLayout.setVisibility(View.GONE);
            }
        }else{
            mTextViewNoticeTitle.setText("");
            mTextViewNoticeTime.setText("");
            mTextViewNoticeContent.setText("暂无公告");
            mImageViewNotice.setVisibility(View.INVISIBLE);
        }
    }
}
